﻿Ext.define('app.prm.project_task.EditQuan',
{
    extend: 'Ext.window.Window',
    modal: true,
    width: 950,
    height: 600,
    projectId: 0,
    scrollable: 'y',
    projectTaskId: 0,
    IsAdd: false,//是新增还是编辑
    IsEdit: false,//是否有界面编辑权限
    IsFollower: false,//是否有关注权限
    是否已关注: false,
    //bodyStyle: 'padding:3px',
    tbar: [],
    initComponent: function () {
        var me = this;
        me._quans = [];
        me._members = [];
        me.items = [me.getTaskForm(), me.getReplyForm(), me.getReplyPanel()];
        if (me.IsAdd)
        {
            me.layout = 'fit';
        }
        me.tbar = [
            { text: '关注并保存', glyph: 0xf0c7, hidden: me.IsAdd || !me.IsFollower || me.是否已关注, handler: function () { me.onSubmit(false, "关注"); } },
            { text: '取消关注', glyph: 0xf0c4, hidden: me.IsAdd || !me.IsFollower || !me.是否已关注, handler: function () { me.onSubmit(false, "取消关注"); } },
            { text: '完成并保存', glyph: 0xf0c7, hidden: me.IsAdd || !me.IsEdit, handler: function () { me.onSubmit(true, null); } },
            '->',
            { text: '保存', glyph: 0xf0c7, hidden: !me.IsEdit, handler: function () { me.onSubmit(false, null); } }
        ];
        me.callParent();
    },
    listeners: {
        afterrender: function (me) {
            if (me.projectTaskId > 0) {
                App.AjaxGet('/api/services/app/projectTask/Get?projectTaskId=' + me.projectTaskId, function (data) {
                    var formatDate = Ext.util.Format.date(data.result.planFinshTime, 'Y-m-d');
                    data.result["planFinshTime"] = formatDate;
                    Ext.getCmp("taskMembers").setValue(data.result.members);
                    Ext.getCmp("quansfanwei").setValue(data.result.quans);
                    Ext.getCmp("txt_follower").setValue(data.result.followers);
                    console.info("任务详细");
                    console.log(data.result);
                    me.getTaskForm().getForm().setValues(data.result);
                    me._members = data.result.memberIds;
                    me._quans = data.result.quanIds;

                    for (var i = 0; i < data.result.children.length; i++) {
                        var child = data.result.children[i];
                        me.saveChild(child.value, child.key)
                    }

                    me.initReply();
                });
            }
        }
    },
    getTaskForm: function () {
        var me = this;
        if (!this.form) {
            this.form = new Ext.form.Panel({
                defaultType: 'textfield',
                title: '任务详细',
                border: true,
               // width: '95%',
                fieldDefaults: {
                    labelWidth: 90,
                    labelAlign: "right",
                    flex: 1,
                    margin: 6
                },
                items: [
                    { xtype: 'hidden', name: 'projectId', value: me.projectId },
                    { xtype: 'hidden', name: 'Id', value: me.projectTaskId },
                    {
                        xtype: 'container',
                        anchor: '98%',
                        layout: 'hbox',
                        items: [
                            {
                                xtype: 'container',
                                flex: 1,
                                layout: 'anchor',
                                items: [
                                     {
                                         xtype: 'textarea',
                                         height: 150,
                                         width: '100%',
                                         allowBlank: false,
                                         fieldLabel: '问题',
                                         name: 'title'
                                     },
                                    {
                                        xtype: 'container',
                                        layout: 'hbox',
                                        items: [
                                            {
                                                xtype: 'textfield',
                                                hidden: !me.IsEdit,
                                                id: 'txt_childtask',
                                                fieldLabel: '子任务'
                                            },
                                            {
                                                xtype: 'button',
                                                style: 'margin:5px 0 0 0',
                                                text: '新增',
                                                hidden: !me.IsEdit,
                                                handler: function () {
                                                    var childTask = Ext.getCmp("txt_childtask");
                                                    var text = childTask.getValue();
                                                    childTask.reset();
                                                    me.saveChild(text, false);
                                                }
                                            }
                                        ]
                                    },
                                    me.getTaskChild()
                                ]
                            },
                            {
                                xtype: 'container',
                                flex: 1,
                                layout: 'anchor',
                                items: [
                                    {
                                        xtype: 'textarea',
                                        height: 50,
                                        width: '98%',
                                        emptyText: '逗号分割',
                                        fieldLabel: '标签',
                                        name: 'tags'
                                    },
                                    {
                                        xtype: 'panel',
                                        style: 'margin:0 0 0 100px',
                                        border: false,
                                        hidden: !me.IsEdit,
                                        html: '<a href="#">选择标签</a>',
                                        listeners: {
                                            afterrender: function (component) {
                                                component.getEl().on('click', function () {
                                                    var win = Ext.create("app.prm.project_task.ChooseTag", { projectId: me.projectId });
                                                    win.on("savesuccess", function (datas) {
                                                        var tags = me.getTaskForm().query("textarea[name=tags]")[0];
                                                        tags.setValue(tags.getValue() + "," + datas);
                                                        win.close();
                                                    });
                                                    win.show();
                                                });
                                            }
                                        }
                                    },
                                    {
                                        xtype: 'container',
                                        flex: 1,
                                        layout: 'hbox',
                                        items: [
                                        {
                                            xtype: 'datefield',
                                            format: 'Y-m-d',
                                            value: new Date(),
                                            allowBlank: false,
                                            fieldLabel: '计划完成时间',
                                            name: 'planFinshTime'
                                        },
                                        {
                                            xtype: 'numberfield',
                                            fieldLabel: '优先级',
                                            name: 'priority',
                                            allowDecimals: false,
                                            value: 0
                                        }
                                        ]
                                    },
                                    {
                                        xtype: 'displayfield',
                                        fieldLabel: '关注者',
                                        id: 'txt_follower',
                                        hidden: me.IsAdd,
                                        value: ''
                                    },
                                     {
                                         xtype: 'displayfield',
                                         id: 'taskMembers',
                                         fieldLabel: '指派人',
                                         value: ''
                                     },
                                     {
                                         xtype: 'displayfield',
                                         id: 'quansfanwei',
                                         fieldLabel: '可见范围',
                                         value: ''
                                     },
                                    {
                                        xtype: 'panel',
                                        style: 'margin:0 0 0 100px',
                                        border: false,
                                        hidden: !me.IsEdit,
                                        html: '<a href="#">设置人员</a>',
                                        listeners: {
                                            afterrender: function (component) {
                                                component.getEl().on('click', function () {
                                                    var win = Ext.create("app.prm.project_task.ChooseQuan", { projectId: me.projectId, projectTaskId: me.projectTaskId });
                                                    win.on("savesuccess", function (memberIds, members, quans, quanIds) {
                                                        Ext.getCmp("taskMembers").setValue(members);
                                                        Ext.getCmp("quansfanwei").setValue(quans);
                                                        me._quans = quanIds;
                                                        me._members = memberIds;
                                                        win.close();
                                                    });
                                                    win.show();
                                                });
                                            }
                                        }
                                    },
                                    {
                                        xtype: 'textfield',
                                        fieldLabel: '工艺',
                                        labelWidth: 40,
                                        width: '98%',
                                        name: 'route'
                                    },
                                    {
                                        xtype: 'container',
                                        layout: 'hbox',
                                        items: [
                                            {
                                                xtype: 'textfield',
                                                labelWidth: 40,
                                                fieldLabel: '步骤',
                                                name: 'step'
                                            },
                                            {
                                                xtype: 'textfield',
                                                labelWidth: 40,
                                                fieldLabel: '部门',
                                                name: 'dept'
                                            }
                                        ]
                                    }
                                ]
                            }
                        ]
                    }
                ]
            });
        }
        return this.form;
    },
    getReplyForm: function () {
        var me = this;
        if (!me.replyform) {
            me.replyform = new Ext.form.Panel({
                border: true,
                split: false,
                title: '问题答复',
                //width: '95%',
                hidden: me.IsAdd || !me.IsEdit,
                style: 'margin:5px 0 0 0',
                bodyStyle: 'padding:3px',
                items: [
                    me.getTextarea(),
                    me.getUploadPanel()
                ],
                bbar: [
                    "->",
                    { text: '提交', icon: '/Content/images/help.png', handler: function () { me.replySave(); } }
                ]
            });
        }
        return me.replyform;
    },
    getReplyPanel: function () {
        var me = this;
        if (!me.replypanel) {
            me.replypanel = new Ext.panel.Panel({
                border: true,
                split: false,
                //width: '95%',
                title: '答复列表',
                hidden: me.IsAdd,
                style: 'margin:5px 0 0 0',
                bodyStyle: 'padding:3px',
                items: []
            });
        }
        return me.replypanel;
    },
    getTextarea: function () {
        if (!this.textarea) {
            this.textarea = new Ext.form.field.TextArea({
                emptyText: '内容',
                width: '98%',
                height: 150
            });
        }
        return this.textarea;
    },
    getUploadPanel: function () {
        var me = this;
        if (!me.uploadFile) {
            me.uploadFile = Ext.create('app.ux.UploadPanel');
        }
        return me.uploadFile;
    },
    getTaskChild: function () {
        var me = this;
        if (!this.taskChild) {
            this.taskChild = new Ext.panel.Panel({
                border: false,
                style: 'margin:0 0 0 100px',
                items: []
            });
        }
        return this.taskChild;
    },
    saveChild: function (text, isChecked) {
        var me = this;

        var panel = new Ext.panel.Panel({
            layout: 'column',
            border: false,
            items: [
                { xtype: 'checkbox', name: 'IsCompleted', boxLabel: text, inputValue: text, checked: isChecked },
                { xtype: 'hidden', name: 'ChildTask', value: text },
                {
                    xtype: 'panel',
                    border: false,
                    hidden: !me.IsEdit,
                    style: 'margin:10px 0 0 0',
                    html: '<span style="text-decoration:underline;cursor:pointer">删除</span>',
                    listeners: {
                        afterrender: function (component) {
                            component.getEl().on('click', function () {
                                me.getTaskChild().remove(panel);
                            });
                        }
                    }
                }
            ]
        });
        me.getTaskChild().add(panel);
    },
    replySave: function () {
        var me = this;

        var title = me.getTextarea().getValue();
        var docs = me.getUploadPanel().getValue();

        var path = "";
        var name = "";
        if (docs.length > 0) {
            path = docs[0].FullPath;
            name = docs[0].Name;
        }

        App.AjaxPost('/api/services/app/projectTask/CreateReply', { ProjectTaskId: me.projectTaskId, Title: title, Name: name, FullPath: path }, function () {
            me.getTextarea().reset();
            me.getUploadPanel().removeAllArry();
            me.initReply();
        });
    },
    initReply: function () {
        var me = this;
        App.AjaxGet('/api/services/app/projectTask/GetReplys?projectTaskId=' + me.projectTaskId, function (data) {
            var result = data.result;
            me.getReplyPanel().removeAll(true);
            for (var i = 0; i < result.length; i++) {
                var reply = result[i];
                var panel = new Ext.panel.Panel({
                    border: false,
                    style: 'margin:5px 0 0 0;',
                    items: [
                        { xtype: 'panel', border: false, style: 'margin:0 0 0 5px', html: '<span style="color:red;font-size:15px">' + reply.createBy + "(" + reply.createOn + ")" + '</span>' },
                        { xtype: 'panel', border: false, style: 'margin:0 0 0 20px', html: reply.note },
                        { xtype: 'panel', border: false, style: 'margin:0 0 0 20px', html: '<a href="/api/services/app/document/GetFile?path=' + reply.fullPath + '" style="color:blue;text-decoration:underline;cursor:pointer">' + reply.name + '</a>' }
                    ]
                });
                me.getReplyPanel().add(panel);
            }
        });
    },
    onSubmit: function (isCompleted, follower) {
        var me = this;

        var json = me.getTaskForm().getValues();
        if (typeof (json.IsCompleted) == "undefined") {
            json.IsCompleted = [];
        } else if (typeof (json.IsCompleted) == "string") {
            json.IsCompleted = [json.IsCompleted];
        }

        if (typeof (json.ChildTask) == "string")
        {
            json.ChildTask = [json.ChildTask];
        }

        json.quans = me._quans;
        json.members = me._members;
        json.Completed = isCompleted;
        json.isFollower = follower;
        App.AjaxPost('/api/services/app/projectTask/EditTaskQuan', json, function () {
            me.fireEvent("savesuccess");
        });
    }
});